#include <bits/stdc++.h>
using namespace std;

int main() {
    int n, h;
    cin >> n;
    vector<int> nxt(n + 1);
    
    for (int i = 1; i <= n; ++i) {
        cin >> nxt[i];
    }
    
    cin >> h;
    vector<int> ans;
    int cur = h;
    
    while (cur) {
        ans.push_back(cur);
        cur = nxt[cur];
    }
    
    for (int i = 0; i < ans.size(); ++i) {
        if (i) cout << " ";
        cout << ans[i];
    }
    
    return 0;
}

